package com.yj.bj.task;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.yj.bj.entity.CardInformation;
import com.yj.bj.entity.MerChants;
import com.yj.bj.service.CardInformationService;
import com.yj.bj.service.MerChantsService;
import com.yj.bj.util.BankCode;
import com.yj.bj.util.HttpClientUtils;
import com.yj.bj.util.YJ;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;

@Controller
@Component
@EnableScheduling
@Lazy(false)
public class SendsNews {
	
	@Autowired
	private CardInformationService cardInformationService;
	@Autowired
	private MerChantsService merChantsService;
	
	@Scheduled(cron="0 0 10 * * ?")
	public void task(){
		System.out.println("hhh");
		String s = YJ.formatDate(new Date());
		CardInformation cardInformation = new CardInformation();
		cardInformation.setCardType("CC");
		cardInformation.setStatementDate(s.substring(s.length()-2,s.length()));
		List<CardInformation> list = cardInformationService.queryObjectForList(cardInformation);
		int t = Integer.parseInt(s.substring(s.length()-2,s.length()-1));
		if(t < 1){
			cardInformation.setStatementDate(s.substring(s.length()-1,s.length()));
			List<CardInformation> cardInformationList = cardInformationService.queryObjectForList(cardInformation);
			list.addAll(cardInformationList);
		}
		for(CardInformation card : list){
			MerChants merChants = new MerChants();
			merChants.setMerChantId(card.getMerChantId());
			merChants = merChantsService.findByObject(merChants);
			String msg = "尊敬的"+merChants.getMerName()+"用户您好，您尾号为"+card.getCardNumber().substring(s.length()-4,s.length())+"的"+ BankCode.getName(card.getIssuingBank())+"账单已出，请及时制定计划还款，感谢您的使用。";
			Map<String, String > map = new HashedMap();
			map.put("merChantId", merChants.getMerChantId());
			map.put("appId", merChants.getAppId());
			map.put("institutionId", merChants.getInstitutionId());
			map.put("msg", msg);
			map.put("type", "1");
			HttpClientUtils.doPost("http://47.104.25.59/templet/JiGuang/transmission.shtml", map);
		}
	}
}
